package com.dashang.util.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

/**
 * <一句话功能简述>
 * <功能详细描述>
 *
 * @author  姓名 工号
 * @version  [版本号, 2011-9-16]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class ValidateCodeServlet extends HttpServlet
{
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1954322536454L;

    /**
     * <一句话功能简述>
     * <功能详细描述>
     * @param fc
     * @param bc
     * @return
     * @see [类、类#方法、类#成员]
     */
    Color getRandColor(int fc, int bc)
    {
        Random random = new Random(new Date().getTime());
        if (fc > 255)
            fc = 255;
        if (bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    /**
     * 重载方法
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        // 设置HEADER信息
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        int width = 75, height = 25;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        // 创建绘制验证码对象！即一系列参数
        Graphics g = image.getGraphics();
        Random random = new Random(new Date().getTime());
        g.setColor(getRandColor(200, 250));
        g.fillRect(1, 1, width - 1, height - 1);
        g.setColor(new Color(102, 102, 102));
        g.drawRect(0, 0, width - 1, height - 1);
        g.setFont(new Font("Times New Roman", Font.TRUETYPE_FONT, 20));
        g.setColor(getRandColor(160, 200));
        for (int i = 0; i < 155; i++)
        {
            int x = random.nextInt(width - 1);
            int y = random.nextInt(height - 1);
            int xl = random.nextInt(6) + 1;
            int yl = random.nextInt(12) + 1;
            g.drawLine(x, y, x + xl, y + yl);
        }

        int x = random.nextInt(width - 1);
        int y = random.nextInt(height - 1);
        int xl = random.nextInt(12) + 1;
        int yl = random.nextInt(6) + 1;
        g.drawLine(x, y, x - xl, y - yl);
        String sRand = "";
        for (int i = 0; i < 4; i++)
        {
            int itmp = random.nextInt(26) + 65;
            char ctmp = (char)itmp;
            sRand += String.valueOf(ctmp);
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            g.drawString(String.valueOf(ctmp), 15 * i + 10, 19);
        }
        HttpSession session = request.getSession();
        session.setAttribute("vcode", sRand);

        // 释放此图形的上下文并释放它所使用的所有系统资源。
        g.dispose();

        // 输出图象到页面
        ImageIO.write(image, "jpeg", response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }
}
